
package com.feib.soeasy.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.PaymentDao;
import com.feib.soeasy.dao.Soez5001Dao;
import com.feib.soeasy.dao.WriteOffPaymentDao;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.model.WriteOffPayment;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.Soez5001Service;
import com.feib.soeasy.util.UserUtil;


/**
 * @title (#)Soez5001ServiceImpl.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/3/7 create by Jimmy Liu<br>
 */
@Service("soez5001Service")
public class Soez5001ServiceImpl extends BaseServiceImpl implements Soez5001Service {

    @Resource(name="soez5001Dao")
    private Soez5001Dao soez5001Dao;

    @Resource(name="writeOffPaymentDao")
    private WriteOffPaymentDao writeOffPaymentDao;

    @Resource(name="paymentDao")
    private PaymentDao paymentDao;

    /*
     *  (non-Javadoc)
     * @see com.feib.soeasy.service.Soez5001Service#queryByGroup(com.feib.soeasy.model.Group, com.feib.soeasy.pagination.PaginatedList)
     */
    @Override
    public PaginatedList<Map<String, Object>> queryByGroup(Group group, PaginatedList<Map<String, Object>> paginationConfig) {

        return  soez5001Dao.queryByGroup(group, paginationConfig);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.Soez5001Service#queryByUSERNO(java.lang.String, java.lang.Long)
     */
    @Override
    public List<Map<String, Object>> queryByUID(String groupNo, String UID) {
        return  soez5001Dao.queryByUID(groupNo, UID);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.Soez5001Service#FbCodeResult(java.lang.Integer, java.lang.Long, java.lang.String)
     */
    @Override
    public int FbCodeResult(Integer caseSwitch, String UID) {
        //return soez5001Dao.FbCodeResult(caseSwitch, UID);
        return 0;
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.Soez5001Service#queryFeedbackLogByPk(java.lang.Long, com.feib.soeasy.model.Group)
     */
    @Override
    public Map<String, Object> queryFeedbackLogByPk(Long feedbackUid, Group group) {
        return soez5001Dao.findFeedbackLogByPk(feedbackUid, group);
    }

    
    @Override
    public void doWriteOffPayment(Long feedbackUid, Long paymentPk, String reason) throws Exception {
        Date now = new Date();
        String note = (null == reason ? "" : reason);
        note = note + "\r\n 由" + UserUtil.getUserGroup().getGroupNo() +"-" + UserUtil.getUserId() +" 於 " + now +" 處理";

        Map<String, Object> feedbackLog =  this.soez5001Dao.findFeedbackLogByPk(feedbackUid);
        //Map<String, Object> payment =  this.soez5001Dao.findPaymentByPk(paymentPk);
        WriteOffPayment writeOffPayment = new WriteOffPayment();
        Long amount = new Long(((Number)feedbackLog.get("TXAMT")).longValue());
        writeOffPayment.setAmount(amount);
        
        String channel = (String)feedbackLog.get("CHANNEL");
        if ("7111111".equalsIgnoreCase(channel))
            channel = "D";
        else if ("TFM".equalsIgnoreCase(channel))
            channel = "E";
        else if ("HILIFE".equalsIgnoreCase(channel))
            channel = "F";
        else if ("OKCVS".equalsIgnoreCase(channel))
            channel = "G";
        else if ("POST".equalsIgnoreCase(channel))
            channel = "H";
        else if ("ACH".equalsIgnoreCase(channel))/* 2012/05/21 顯示ACH通路資料    */
            channel = "I";
        
        writeOffPayment.setChannel(channel);
        
        writeOffPayment.setConfirmDate(now);
        writeOffPayment.setConfirmUser(UserUtil.getUser());
        
        Payment payment1 = new Payment();
        payment1.setPaymentPk(paymentPk);
        writeOffPayment.setPayment(payment1);
        writeOffPayment.setRlFlag(Boolean.FALSE);
        writeOffPayment.setWriteOffDate(now);
        writeOffPayment.setWriteOffStatus(WriteOffPayment.WRITEOFF_STATUS_OK);
        writeOffPayment.setWriteOffUser(UserUtil.getUser());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        writeOffPayment.setTxDate(dateFormat.parse((String)feedbackLog.get("ADAY")));
        writeOffPayment.setFeedbackUid(feedbackUid);
        writeOffPayment.setWriteOffNote(reason);
        this.writeOffPaymentDao.save(writeOffPayment);
        
        Payment payment = paymentDao.findByPk(paymentPk);
        payment.setWriteOffPayment(writeOffPayment);
        payment.setPaymentStatus(Payment.PAYMENT_STATUS_CLOSED);
        paymentDao.save(payment);
        
        soez5001Dao.updateFeedbackLogResultCode(new Integer(1), feedbackUid, reason); //正常處理
        
    }

    @Override
    public void doSkipFeedbackLog(Long feedbackUid, String reason) {
        String note = (null == reason ? "" : reason);
        note = note + "\r\n 由" + UserUtil.getUserGroup().getGroupNo() +"-" + UserUtil.getUserId() +" 於 " + new Date() +" 處理";
        soez5001Dao.updateFeedbackLogResultCode(new Integer(3), feedbackUid, reason); //忽略處理
        
    }

}
